package com.mmall.admin.shiro;

import com.mmall.common.base.ServerResponse;
import com.mmall.common.util.FilterOutUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.session.SessionException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.web.filter.authc.LogoutFilter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

/**
 * 退出登录 filter
 *
 * @author gg
 * @version LogoutFormFilter.java, v 0.1 2019-05-10 10:06 gg
 */
@Slf4j
public class LogoutFormFilter extends LogoutFilter {

    @Override
    protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
        ShiroBaseFilter shiroBaseFilter = new ShiroBaseFilter();
        boolean result = shiroBaseFilter.preHandle(request, response);
        if (result) {
            return true;
        } else {
            Subject subject = getSubject(request, response);
            try {
                subject.logout();
            } catch (SessionException ise) {
                log.debug("Encountered session exception during logout.  This can generally safely be ignored.", ise);
            }
            ServerResponse error = ServerResponse.createByErrorCodeMessage(403, "请先进行登录");
            FilterOutUtil.out(error, response);
            return false;
        }
    }
}